package com.xk857.school.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xk857.config.AuthUtil;
import com.xk857.entities.Courier;
import com.xk857.entities.SysUser;
import com.xk857.feign.IFeignSystemController;
import com.xk857.school.mapper.CourierMapper;
import com.xk857.school.req.CourierREQ;
import com.xk857.school.service.ICourierService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xk857.util.base.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 代取快递 服务实现类
 * </p>
 *
 * @author CV大魔王
 * @since 2021-05-02
 */
@Primary
@Service
public class CourierServiceImpl extends ServiceImpl<CourierMapper, Courier> implements ICourierService {

    @Autowired
    private IFeignSystemController feignSystemController;

    @Override
    public Result add(Courier courier) {
        // 如果没有传递发布人id，则根据token获取
        if (StringUtils.isEmpty(courier.getUserId())){
            SysUser sysUser = AuthUtil.getUserInfo();
            courier.setUserId(sysUser.getId());
        }
        if (StringUtils.isEmpty(courier.getStaus())){
           courier.setStaus("1");
        }
        // 查询发布人相关信息
        SysUser user = feignSystemController.findUserByUserId(courier.getUserId());
        courier.setUserImg(user.getImageUrl());
        courier.setNickName(user.getNickName());
        // 创建
        baseMapper.insert(courier);
        return Result.ok("发布成功");
    }

    @Override
    public Result update(Courier courier) {
        courier.setUpdateTime(new Date());
        baseMapper.updateById(courier);
        return Result.ok("更改成功");
    }

    @Override
    public Result delete(String id) {
        Courier courier = baseMapper.selectById(id);
        courier.setStaus("0");
        baseMapper.updateById(courier);
        return Result.ok("删除成功");
    }

    @Override
    public Result helpByPeople(String id, String peopleId, String peopleRemark) {
        // 查询帮取快递用户信息
        SysUser sysUser = feignSystemController.findUserByUserId(peopleId);
        // 查询快递详细信息
        Courier courier = baseMapper.selectById(id);
        courier.setPeopleId(peopleId);
        courier.setPeopleImg(sysUser.getImageUrl());
        courier.setPeopleNick(sysUser.getNickName());
        courier.setPeopleRemark(peopleRemark);
        // 本次交易结束，结束时间
        courier.setCompleteTime(new Date());

        courier.setStaus("2");
        baseMapper.updateById(courier);
        return Result.ok();
    }

    @Override
    public Result searchPagePub(CourierREQ req) {
        QueryWrapper<Courier> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(req.getUserId())) {
            wrapper.eq("user_id", req.getUserId());
            wrapper.ne("staus", "0");
        }
        System.out.println(req.toString());
        if (StringUtils.isNotEmpty(req.getClassName())) {
            wrapper.eq("class_name", req.getClassName());
        }
        if (StringUtils.isNotEmpty(req.getSchoolName())) {
            wrapper.eq("school_name", req.getSchoolName());
        }
        if (StringUtils.isNotEmpty(req.getId())) {
            wrapper.eq("id", req.getId());
        }
        // 只查询用户发布的
        if (StringUtils.isEmpty(req.getUserId())) {
            wrapper.eq("staus", "1").or().eq("staus", "2");
        }

        wrapper.orderByDesc("create_time");
        return Result.ok(baseMapper.selectPage(req.getPage(), wrapper));
    }

    @Override
    public Result searchPage(CourierREQ req) {
        QueryWrapper<Courier> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(req.getUserId())) {
            wrapper.eq("user_id", req.getUserId());
        }
        if (StringUtils.isNotEmpty(req.getClassName())) {
            wrapper.like("class_name", req.getClassName());
        }
        if (StringUtils.isNotEmpty(req.getSchoolName())) {
            wrapper.like("school_name", req.getSchoolName());
        }
        if (StringUtils.isNotEmpty(req.getId())) {
            wrapper.eq("id", req.getId());
        }
        // 只查询用户发布的
        if (StringUtils.isNotEmpty(req.getStatus())) {
            wrapper.eq("staus", req.getStatus());
        }
        wrapper.orderByDesc("create_time");
        return Result.ok(baseMapper.selectPage(req.getPage(), wrapper));
    }

    @Override
    public Result complete(String id) {
        // 从token获取用户信息
        SysUser sysUser = AuthUtil.getUserInfo();
        Courier courier = baseMapper.selectById(id);
        if (!courier.getUserId().equals(sysUser.getId())){
            return Result.error("您无权修改信息");
        }
        courier.setStaus("3");
        baseMapper.updateById(courier);
        return Result.ok();
    }

    @Override
    public Result getAllSchoolNameGroup() {
        QueryWrapper<Courier> wrapper = new QueryWrapper<>();
        wrapper.groupBy("school_name");
        List<String> schoolNameList = new ArrayList<>();
        for (Courier courier : baseMapper.selectList(wrapper)) {
            schoolNameList.add(courier.getSchoolName());
        }
        return Result.ok(schoolNameList);
    }

    @Override
    public Result getAllClassGroup() {
        QueryWrapper<Courier> wrapper = new QueryWrapper<>();
        wrapper.groupBy("class_name");
        List<String> classList = new ArrayList<>();
        for (Courier courier : baseMapper.selectList(wrapper)) {
            classList.add(courier.getClassName());
        }
        return Result.ok(classList);
    }
}
